<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Platform extends MY_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('platform_model', 'platform');
	}
	/**
	 * 平台管理
	 * @return [type] [description]
	 */
	public function index() {
		$this->load->library('pagination');
		$perPage = 3;

		$config['base_url'] = site_url('admin/platform/index');
		$config['total_rows'] = $this->db->count_all_results('platform');
		$config['per_page'] = $perPage;
		$config['uri_segment'] = 4;
		$config['first_link'] = '第一页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$config['last_link'] = '最后一页';

		$this->pagination->initialize($config);
		$this->_data['links'] = $this->pagination->create_links();

		$offset = $this->uri->segment(4);
		$this->db->limit($perPage, $offset);

		$this->_data['platforms'] = $this->platform->fetch_all();
		$this->load->view('admin/platform/index.html', $this->_data);
	}


	public function posts() {
		$platform_id = $this->uri->segment(4);
		if (FALSE == $platform_id) {
			$this->_write();
		} else {
			$this->_edit($platform_id);
		}
	}

	private function _write() {
		if ( empty($_POST) ) {
			$this->load->view('admin/platform/form.html');
		} else {
			$data = $this->input->post();
			$result = $this->platform->add($data);
			redirect('admin/platform/index');
		}
	}

	private function _edit($platform_id) {
		if (empty($_POST)) {
			$data = $this->platform->show($platform_id);
			$this->_data['platform'] = $data;
			$this->load->view('admin/platform/form.html', $this->_data);

		} else {
			$data = $this->input->post();
			$this->platform->edit($platform_id, $data);
			redirect('admin/platform/index');
		}
	}

}